﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Markup;

using Silversocial.Client.Modules;

using Silverstone;

namespace Silversocial.Client.Views
{
    /// <summary>
    ///     Create a base class to derive our page from.  This is because we cannot specify a generic
    ///     in XAML
    /// </summary>
    [ContentProperty("Content")]
    [TemplateVisualState(Name = "AwaitingLogin", GroupName = "LoginStates")]
    [TemplateVisualState(Name = "LoggingIn", GroupName = "LoginStates")]
    public class LoginBasePage : PageView<LoginViewModel>
    {
        /// <summary>
        ///     Redefine the base class's content property to keep the designers happy
        /// </summary>
        public new UIElement Content
        {
            get { return base.Content; }
            set { base.Content = value; }
        }
    }

    [TemplateVisualState(Name = "AwaitingLogin", GroupName = "LoginStates")]
    [TemplateVisualState(Name = "LoggingIn", GroupName = "LoginStates")]
    public partial class LoginPage : ILoginView
    {
        public LoginPage()
        {
            InitializeComponent();
        }

        public override void OnLoad()
        {
            VisualStateManager.GoToState(this, "AwaitingLogin", false);
            this.ViewModel.PropertyChanged += HandleViewModelPropertyChanged;
        }

        public void LoginUnsuccessful()
        {
            HtmlPage.Window.Alert("Your details were incorrect");
        }

        private void HandleViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsLoggingIn")
            {
                if (this.ViewModel.IsLoggingIn)
                {
                    VisualStateManager.GoToState(this, "LoggingIn", true);
                }
                else
                {
                    VisualStateManager.GoToState(this, "AwaitingLogin", true);
                }
            }
        }
    }
}
